<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/
        function ListNode(val, next) {
            this.val = (val === undefined ? 0 : val)
            this.next = (next === undefined ? null : next)
        }
        var deleteDuplicates = function(head) {
            if (head == null) {
                return null;
            }
            let header = new ListNode(0, null);
            header.next = head;
            let cur = head;
            let pre = header;
            let first = false;
            while (cur.next != null) {
                if (cur.val === cur.next.val) {
                    cur.next = cur.next.next
                    first = true;
                } else {
                    if (first) {
                        // 还要删除这个重复的节点
                        let tmp = cur.next;
                        pre.next = cur.next;
                        cur = tmp;
                    } else {
                        // pre 始终为 cur 前面的一个节点
                        pre = cur;
                        cur = cur.next;
                    }
                }
            }
            return header.next;
        };

        let node1 = new ListNode(1, null);
        let node2 = new ListNode(1, null);
        let node3 = new ListNode(1, null);
        let node4 = new ListNode(2, null);
        let node5 = new ListNode(3, null);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        deleteDuplicates(node1);
    </script>
</body>
</html>